package com.syni.mdd.sjb.tool.controller.protect;

import com.syni.mdd.sjb.common.component.mybatis.common.entity.DxshAppUserInfo;
import com.syni.mdd.sjb.common.component.mybatis.common.mapper.DxshAppUserInfoMapper;
import com.syni.mdd.sjb.common.component.utils.common.spring.ApplicationContextHolder;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.tool.entity.vo.requestVo.GetVerificationCodeVo;
import com.syni.mdd.sjb.tool.utils.SmsUtils;
import com.syni.mdd.sjb.tool.utils.VerificationCodeUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/common/verificationCode")
public class VerificationCodeController {

    @Autowired
    SmsUtils smsUtils;

    @Autowired
    DxshAppUserInfoMapper dxshAppUserInfoMapper;

    @PostMapping("/getVerificationCode")
    @ApiOperation(value = "获取验证码")
    public Map<String,Object> getVerificationCode(@RequestBody GetVerificationCodeVo getVerificationCodeVo){
        String phone = getVerificationCodeVo.getPhone();
        Integer codeType = getVerificationCodeVo.getCodeType();
        Integer channel = getVerificationCodeVo.getChannel();
        //添加进redis
        String code = VerificationCodeUtils.getVerificationCode();
        VerificationCodeUtils.addVerificationCode(channel,phone,codeType,code);
        if(ApplicationContextHolder.checkRunProfile(ApplicationContextHolder.RUN_PRO)){
            smsUtils.sendSmsVerificationCode(channel,phone,code);
        }
        Map<String,Object> resultMap = new HashMap<>();
        if(!ApplicationContextHolder.checkRunProfile(ApplicationContextHolder.RUN_PRO)) {
            resultMap.put("verificationCode", code);
        }
        return ResultMapHelper.success(resultMap,"获取成功");
    }

    @PostMapping("/checkVerificationCode")
    @ApiOperation(value = "校验验证码")
    public Map<String,Object> checkVerificationCode(
            @RequestBody GetVerificationCodeVo getVerificationCodeVo){
//        boolean check = VerificationCodeUtils.checkVerificationCode(getVerificationCodeVo.getChannel(),
//                getVerificationCodeVo.getPhone(),
//                getVerificationCodeVo.getCodeType(),getVerificationCodeVo.getVerificationCode());
//        Map<String,Integer> checkResult = new HashMap<>();
//        checkResult.put("result",check ? 1 : 0);
        DxshAppUserInfo dxshAppUserInfo = new DxshAppUserInfo();
        dxshAppUserInfo.setUserId(99999999);
        dxshAppUserInfoMapper.insert(dxshAppUserInfo);
        int i = 1/0;
        return ResultMapHelper.success(null);
    }
}
